/**
 * @Copyright:Copyright (c) 2018 - 2100
 * @Company:gome
 */

package com.gome.gop.common;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Title:
 * @Description:
 * @Author:chengcheng7
 * @Since:2020年6月2日 上午10:46:12
 * @Copyright:Copyright (c) 2018 - 2100
 * @Company:gome
 * @Version:1.1.0
 */
public class JackJsonUtil {
    
    private static ObjectMapper objectMapper;
    
    private JackJsonUtil() {
    }
    
    public static ObjectMapper getInstance() {
        
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
            objectMapper.setSerializationInclusion(Include.NON_NULL);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper;
        }
        return objectMapper;
        
    }
    
    /**
     * 解析一个对象
     * 
     * @param jsonString
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T readValue(String jsonString, Class<T> clazz) {
        
        T t = null;
        try {
            t = JackJsonUtil.getInstance().readValue(jsonString, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return t;
        
    }
    
    
    /**
     * 对象转换字符串
     * @param value
     * @return
     */
    public static String writeValueAsString(Object value) {
        try {
            return JackJsonUtil.getInstance().writeValueAsString(value);
        }
        catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
}
